package com.lebang.medicalInquiry.service;

import com.alibaba.fastjson.JSONObject;
import com.google.common.primitives.Doubles;
import com.lebang.medicalInquiry.constant.ConsultationScheduleEnum;
import com.lebang.medicalInquiry.mapper.*;
import com.lebang.medicalInquiry.model.po.*;
import com.lebang.medicalInquiry.model.vo.ConsultationSheetInfoVo;
import com.lebang.medicalInquiry.model.vo.ConsultationSheetStatisticsVo;
import com.lebang.medicalInquiry.model.vo.ConsultationSheetVo;
import com.lebang.medicalInquiry.model.vo.PhysicianInfoVo;
import com.lebang.medicalInquiry.utils.TextUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class OrganizationService {

    @Resource
    private ConsultationSheetMapper consultationSheetMapper;

    @Resource
    private ConsultationSheetScheduleMapper consultationSheetScheduleMapper;

    @Resource
    private UserPatientMapper userPatientMapper;

    @Resource
    private UserPatientCaseFileMapper userPatientCaseFileMapper;

    @Resource
    private PhysicianMapper physicianMapper;

    @Resource
    private UserMapper userMapper;

    public List<ConsultationSheetInfoVo> getConsultationSheet(String operatorUuid, String status, Integer page, Integer limit) {
        return consultationSheetMapper.getConsultationSheetList(null, operatorUuid, status, (page - 1) * limit, limit);
    }

    /**
     * 返回对应进度的数量
     *
     * @param operatorUuid 运营人员uuid
     * @param status       多个进度类型
     * @return
     */
    public List<ConsultationSheetStatisticsVo> countConsultationSheetNumber(String operatorUuid, String status) {
        List<ConsultationSheetStatisticsVo> consultationSheetStatisticsVos = consultationSheetMapper.countByOperatorUuidAndStatus(operatorUuid, Arrays.asList(status.split(",")));
        List<ConsultationSheetStatisticsVo> countByType = consultationSheetMapper.countByType(operatorUuid);
        consultationSheetStatisticsVos.addAll(countByType);


        return consultationSheetStatisticsVos;
    }


    public ConsultationSheetInfoVo getConsultationSheetById(Integer consultationSheetId) {
        return consultationSheetMapper.getInfoById(consultationSheetId);

    }

    public ConsultationSheet getById(Integer consultationSheetId) {
        return consultationSheetMapper.selectByPrimaryKey(consultationSheetId);

    }

    @Transactional(rollbackFor = Exception.class)
    public void addConsultationSheet(ConsultationSheet consultationSheet, ConsultationSheetVo consultationSheetScheduleVo) {
        ConsultationSheetSchedule consultationSheetSchedule = ConsultationSheetVo.buildSheetSchedule(consultationSheetScheduleVo);
        // 同步问诊单状态
        consultationSheet.setScheduleState(consultationSheetSchedule.getScheduleState());
        if (ConsultationScheduleEnum.settle_bill.getCode().equals(consultationSheetSchedule.getScheduleState())) {
            // 如果是不接诊和待结算 需要提交最终账单 修改问诊单
            if (!TextUtil.isEmpty(consultationSheetSchedule.getValueJson())) {
                Map<String, BigDecimal> map = JSONObject.parseObject(consultationSheetSchedule.getValueJson(), Map.class);
                if (map.size() > 0) {
                    final BigDecimal[] bigDecimal = {BigDecimal.valueOf(0)};
                    map.values().forEach(s -> {
                        bigDecimal[0] = bigDecimal[0].add(BigDecimal.valueOf(Double.parseDouble(s.toString())));
                    });
                    consultationSheet.setUltimatelyBillJson(consultationSheetSchedule.getValueJson());
                    consultationSheet.setImgs(consultationSheetScheduleVo.getImgs());
                    consultationSheet.setUltimatelyMoney(bigDecimal[0]);
                }
            }

        }
        consultationSheetMapper.updateByPrimaryKeySelective(consultationSheet);
        consultationSheetScheduleMapper.insertSelective(consultationSheetSchedule);
    }

    public static void main(String[] args) {
        BigDecimal bigDecimal = BigDecimal.valueOf(0);
        bigDecimal = bigDecimal.add(BigDecimal.valueOf(10));
        System.out.println(bigDecimal);
    }


    // 查询用户的就诊人
    public List<UserPatient> getUserPatientList(String uuid, String ids, Integer page, Integer limit) {
        return userPatientMapper.getUserPatientList(uuid, Arrays.asList(ids.split(",")).stream().map(Integer::valueOf).collect(Collectors.toList()), (page - 1) * limit, limit);
    }

    /**
     * 根据uuid和患者id 查询就诊人上传的病例文件
     *
     * @param uuid
     * @param patientId
     * @return
     */
    public List<UserPatientCaseFile> getUserPatientCaseFileList(String uuid, Integer patientId, Integer page, Integer limit) {
        return userPatientCaseFileMapper.getUserPatientCaseFileList(uuid, patientId, (page - 1) * limit, limit);
    }

    public List<PhysicianInfoVo> getPhysicianList(String uuid) {
        return physicianMapper.getListByOrganizationId(uuid);
    }

    public List<ConsultationSheetSchedule> getConsultationScheduleList(Integer sheetId) {
        return consultationSheetMapper.getConsultationScheduleList(sheetId);
    }


    public Integer addConsultationScheduleList(ConsultationSheet consultationSheet) {
        if (consultationSheet.getId() != null) {
            consultationSheetMapper.updateByPrimaryKeySelective(consultationSheet);
        } else {
            consultationSheet.setScheduleState(ConsultationScheduleEnum.wait_prepay.getCode());
            consultationSheetMapper.insertSelective(consultationSheet);
        }

        return consultationSheet.getId();
    }

    public Map<String, User> getUserHeadImg(List<String> uuids) {
        List<User> users = userMapper.getUserHeadImgByUuids(uuids);
        return users.stream().collect(Collectors.toMap(User::getUuid, Function.identity()));
    }
}
